home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / c_tutor.arc / TEXT.ARC / CHAP11.TXT < prev    next >
Text File  |  1990-08-08  |  29KB  |  653 lines

  1.  
  2.                       Chapter 11 - Structures and Unions
  3.  
  4.  
  5.                             WHAT IS A STRUCTURE?
  6.  
  7.              A structure is a user defined data type.   You have the
  8.         ability  to  define  a new type of  data  considerably  more
  9.         complex than the types we have been using.  A structure is a
  10.         combination  of  several different previously  defined  data
  11.         types,  including other structures we have defined.  An easy
  12.         to  understand definition is,  a structure is a grouping  of
  13.         related  data in a way convenient to the programmer or  user
  14.         of the program.   The best way to understand a structure  is
  15.         to  look  at  an example,  so if you will load  and  display
  16.         STRUCT1.C, we will do just that.
  17.  
  18.              The  program begins with a structure  definition.   The
  19.         key  word  "struct"  is followed by  some  simple  variables
  20.         between  the  braces,   which  are  the  components  of  the
  21.         structure.   After  the  closing brace,  you will  find  two
  22.         variables listed,  namely "boy",  and "girl".   According to
  23.         the  definition  of a structure,  "boy" is  now  a  variable
  24.         composed of three elements,  "initial",  "age", and "grade".
  25.         Each of the three fields are associated with "boy", and each
  26.         can  store a variable of its respective type.   The variable
  27.         "girl"  is also a variable containing three fields with  the
  28.         same  names  as those of "boy" but  are  actually  different
  29.         variables.  We have therefore defined 6 simple variables.
  30.  
  31.                          A SINGLE COMPOUND VARIABLE
  32.  
  33.              Lets  examine  the  variable "boy"  more  closely.   As
  34.         stated above, each of the three elements of "boy" are simple
  35.         variables  and can be used anywhere in a C program  where  a
  36.         variable of their type can be used.   For example, the "age"
  37.         element  is  an integer variable and can therefore  be  used
  38.         anywhere  in a C program where it is legal to use an integer
  39.         variable,  in calculations, as a counter, in I/O operations,
  40.         etc.   The  only problem we have is defining how to use  the
  41.         simple  variable  "age"  which is a  part  of  the  compound
  42.         variable  "boy".   We  use both names with a  decimal  point
  43.         between  them with the major name first.  Thus "boy.age"  is
  44.         the  complete  variable name for the "age" field  of  "boy".
  45.         This  construct can be used anywhere in a C program that  it
  46.         is desired to refer to this field.   In fact,  it is illegal
  47.         to  use the name "boy" or "age" alone because they are  only
  48.         partial definitions of the complete field.  Alone, the names
  49.         refer to nothing.
  50.  
  51.                      ASSIGNING VALUES TO THE VARIABLES
  52.  
  53.              Using  the above definition,  we can assign a value  to
  54.         each  of  the  three fields of "boy" and each of  the  three
  55.         fields  of  "girl".   Note carefully that  "boy.initial"  is
  56.  
  57.  
  58.                                   Page 77
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.                       Chapter 11 - Structures and Unions
  69.  
  70.  
  71.         actually  a "char" type variable,  because it  was  assigned
  72.         that in the structure, so it must be assigned a character of
  73.         data.   Notice  that "boy.initial" is assigned the character
  74.         'R'  in agreement with the above rules.   The remaining  two
  75.         fields of "boy" are assigned values in accordance with their
  76.         respective  types.   Finally  the three fields of  girl  are
  77.         assigned values but in a different order to illustrate  that
  78.         the order of assignment is not critical.
  79.  
  80.                      HOW DO WE USE THE RESULTING DATA?
  81.  
  82.              Now  that  we  have assigned values to the  six  simple
  83.         variables, we can do anything we desire with them.  In order
  84.         to keep this first example simple,  we will simply print out
  85.         the values to see if they really do exist as  assigned.   If
  86.         you carefully inspect the "printf" statements,  you will see
  87.         that there is nothing special about them.  The compound name
  88.         of each variable is specified because that is the only valid
  89.         name by which we can refer to these variables.
  90.  
  91.              Structures  are  a very useful method of grouping  data
  92.         together  in  order to make a program easier  to  write  and
  93.         understand.   This  first example is too simple to give  you
  94.         even  a hint of the value of using structures,  but continue
  95.         on  through  these lessons and eventually you will  see  the
  96.         value of using structures.
  97.  
  98.              Compile and run STRUCT1.C and observe the output.
  99.  
  100.                            AN ARRAY OF STRUCTURES
  101.  
  102.              Load  and  display the next  program  named  STRUCT2.C.
  103.         This  program  contains  the same  structure  definition  as
  104.         before  but  this  time we define an array of  12  variables
  105.         named "kids".   This program therefore contains 12 times 3 =
  106.         36  simple variables,  each of which can store one  item  of
  107.         data  provided  that  it is of the correct  type.   We  also
  108.         define a simple variable named "index" for use in the  "for"
  109.         loops.
  110.  
  111.              In order to assign each of the fields a value, we use a
  112.         "for"  loop  and  each  pass through  the  loop  results  in
  113.         assigning a value to three of the fields.  One pass  through
  114.         the  loop assigns all of the values for one of  the  "kids".
  115.         This would not be a very useful way to assign data in a real
  116.         situation, but a loop could read the data in from a file and
  117.         store it in the correct fields.  You might consider this the
  118.         crude beginning of a data base, which it is.
  119.  
  120.              In  the next few instructions of the program we  assign
  121.         new  values to some of the fields to illustrate  the  method
  122.  
  123.  
  124.                                   Page 78
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.                       Chapter 11 - Structures and Unions
  135.  
  136.  
  137.         used to accomplish this.  It should be self explanatory,  so
  138.         no additional comments will be given.
  139.  
  140.                        A RECENT UPGRADE TO THE C LANGUAGE
  141.  
  142.              Turbo C allows you to copy an entire structure with one
  143.         statement.  This  is  a  fairly recent  addition  to  the  C
  144.         language and will be a part of the ANSI standard when it  is
  145.         published, so you should feel free to use it with your Turbo
  146.         C  compiler.   Line 22 is an example of  using  a  structure
  147.         assignment.  In this statement, all 3 fields of kids[4]  are
  148.         copied into their respective fields of kids[10].
  149.  
  150.                    WE FINALLY DISPLAY ALL OF THE RESULTS
  151.  
  152.              The  last few statements contain a "for" loop in  which
  153.         all  of  the generated values are displayed in  a  formatted
  154.         list.   Compile and run the program to see if it  does  what
  155.         you expect it to do.
  156.  
  157.                    USING POINTERS AND STRUCTURES TOGETHER
  158.  
  159.              Load  and  display  the  file named  STRUCT3.C  for  an
  160.         example of using pointers with structures.   This program is
  161.         identical  to the last program except that it uses  pointers
  162.         for some of the operations.
  163.  
  164.              The  first  difference shows up in  the  definition  of
  165.         variables  following  the  structure  definition.   In  this
  166.         program  we define a pointer named "point" which is  defined
  167.         as  a  pointer that points to the structure.   It  would  be
  168.         illegal  to  try to use this pointer to point to  any  other
  169.         variable  type.   There  is a very definite reason for  this
  170.         restriction  in  C as we have alluded to  earlier  and  will
  171.         review in the next few paragraphs.
  172.  
  173.              The  next difference is in the "for" loop where we  use
  174.         the pointer for accessing the data fields.  Since "kids"  is
  175.         a  pointer  variable that points to the  structure,  we  can
  176.         define "point" in terms of "kids".  The variable "kids" is a
  177.         constant so it cannot be changed in value, but "point" is  a
  178.         pointer  variable and can be assigned any  value  consistent
  179.         with  its being required to point to the structure.   If  we
  180.         assign  the  value of "kids" to "point" then  it  should  be
  181.         clear that it will point to the first element of the  array,
  182.         a structure containing three fields.
  183.  
  184.                              POINTER ARITHMETIC
  185.  
  186.              Adding  1 to "point" will now cause it to point to  the
  187.         second  field of the array because of the way  pointers  are
  188.  
  189.  
  190.                                   Page 79
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.                       Chapter 11 - Structures and Unions
  201.  
  202.  
  203.         handled in C.   The system knows that the structure contains
  204.         three  variables  and it knows how many memory elements  are
  205.         required to store the complete structure.   Therefore if  we
  206.         tell it to add one to the pointer,  it will actually add the
  207.         number  of  memory  elements  required to get  to  the  next
  208.         element of the array.   If, for example, we were to add 4 to
  209.         the  pointer,  it would advance the value of the  pointer  4
  210.         times the size of the structure,  resulting in it pointing 4
  211.         elements  farther  along the array.   This is the  reason  a
  212.         pointer  cannot be used to point to any data type other than
  213.         the one for which it was defined.
  214.  
  215.              Now to return to the program displayed on your monitor.
  216.         It  should be clear from the previous discussion that as  we
  217.         go through the loop, the pointer will point to the beginning
  218.         of  one of the array elements each time.   We can  therefore
  219.         use  the  pointer to reference the various elements  of  the
  220.         structure.   Referring to the elements of a structure with a
  221.         pointer occurs so often in C that a special method of  doing
  222.         that  was  devised.   Using "point->initial" is the same  as
  223.         using  "(*point).initial" which is really the way we did  it
  224.         in  the  last  two programs.  Remember that  *point  is  the
  225.         stored  data to which the pointer points and  the  construct
  226.         should be clear.  The "->" is made up of the minus sign  and
  227.         the greater than sign.
  228.  
  229.              Since the pointer points to the structure, we must once
  230.         again  define which of the elements we wish to refer to each
  231.         time  we use one of the elements of  the  structure.   There
  232.         are, as we have seen, several different methods of referring
  233.         to the members of the structure, and in the "for" loop  used
  234.         for output at the end of the program, we use three different
  235.         methods.   This  would be considered very  poor  programming
  236.         practice,  but  is done this way here to illustrate  to  you
  237.         that  they all lead to the same result.  This  program  will
  238.         probably   require  some  study  on  your  part   to   fully
  239.         understand,  but  it will be worth your time and  effort  to
  240.         grasp these principles.
  241.  
  242.              Lines  29  and  30  are  two  additional  examples   of
  243.         structure assignment for your benefit.
  244.  
  245.              Compile and run this program.
  246.  
  247.                         NESTED AND NAMED STRUCTURES
  248.  
  249.              Load and display the file named NESTED.C for an example
  250.         of  a nested structure.   The structures we have seen so far
  251.         have been very simple,  although useful.   It is possible to
  252.         define  structures  containing dozens and even  hundreds  or
  253.         thousands  of  elements but it would be to  the  programmers
  254.  
  255.  
  256.                                   Page 80
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.                       Chapter 11 - Structures and Unions
  267.  
  268.  
  269.         advantage not to define all of the elements at one pass  but
  270.         rather to use a hierarchical structure of definition.   This
  271.         will be illustrated with the program on your monitor.
  272.  
  273.              The  first  structure  contains three elements  but  is
  274.         followed by no variable name.  We therefore have not defined
  275.         any variables only a structure, but since we have included a
  276.         name  at the beginning of the structure,  the  structure  is
  277.         named  "person".   The name "person" can be used to refer to
  278.         the  structure  but not to any variable  of  this  structure
  279.         type.   It is therefore a new type that we have defined, and
  280.         we can use the new type in nearly the same way we use "int",
  281.         "char",  or  any  other  types that exist in  C.   The  only
  282.         restriction is that this new name must always be  associated
  283.         with the reserved word "struct".
  284.  
  285.              The  next  structure definition contains  three  fields
  286.         with the middle field being the previously defined structure
  287.         which we named "person".  The variable which has the type of
  288.         "person" is named "descrip".   So the new structure contains
  289.         two   simple   variables,   "grade"  and  a   string   named
  290.         "lunch[25]",  and  the  structure  named  "descrip".   Since
  291.         "descrip"  contains  three  variables,   the  new  structure
  292.         actually contains 5 variables.  This structure is also given
  293.         a name "alldat",  which is another type definition.  Finally
  294.         we  define an array of 53 variables each with the  structure
  295.         defined by "alldat",  and each with the name "student".   If
  296.         that is clear,  you will see that we have defined a total of
  297.         53 times 5 variables,  each of which is capable of storing a
  298.         value.
  299.  
  300.                              TWO MORE VARIABLES
  301.  
  302.              Since  we  have a new type definition we can use it  to
  303.         define  two  more variables.   The variables  "teacher"  and
  304.         "sub"  are  defined in line 16 to be variables of  the  type
  305.         "alldat",  so  that each of these two  variables  contain  5
  306.         fields which can store data.
  307.  
  308.                        NOW TO USE SOME OF THE FIELDS
  309.  
  310.              In  the next five lines of the program,  we will assign
  311.         values to each of the fields of "teacher".   The first field
  312.         is  the  "grade" field and is handled just  like  the  other
  313.         structures  we  have studied because it is not part  of  the
  314.         nested structure.  Next we wish to assign a value to her age
  315.         which  is  part of the nested structure.   To  address  this
  316.         field  we start with the variable name "teacher" to which we
  317.         append  the name of the group "descrip",  and then  we  must
  318.         define which field of the nested structure we are interested
  319.         in,  so  we append the name "age".   The teachers status  is
  320.  
  321.  
  322.                                   Page 81
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.                       Chapter 11 - Structures and Unions
  333.  
  334.  
  335.         handled in exactly the same manner as her age,  but the last
  336.         two  fields  are  assigned  strings using  the  string  copy
  337.         "strcpy" function which must be used for string  assignment.
  338.         Notice  that the variable names in the "strcpy" function are
  339.         still variable names even though they are made up of several
  340.         parts each.
  341.  
  342.              The variable "sub" is assigned nonsense values in  much
  343.         the  same  way,  but in a different order since they do  not
  344.         have to occur in any required order.   Finally, a few of the
  345.         "student"  variables  are assigned values  for  illustrative
  346.         purposes  and  the program ends.   None of  the  values  are
  347.         printed  for illustration since several were printed in  the
  348.         last examples.
  349.  
  350.                            MORE ABOUT STRUCTURES
  351.  
  352.              It is possible to continue nesting structures until you
  353.         get  totally confused.   If you define  them  properly,  the
  354.         computer  will  not get confused because there is no  stated
  355.         limit as to how many levels of nesting are  allowed.   There
  356.         is probably a practical limit of three beyond which you will
  357.         get confused, but the language has no limit.  In addition to
  358.         nesting, you can include as many structures as you desire in
  359.         any level of structures,  such as defining another structure
  360.         prior  to  "alldat" and using it in "alldat" in addition  to
  361.         using  "person".   The  structure named  "person"  could  be
  362.         included in "alldat" two or more times if desired,  as could
  363.         pointers to it.
  364.  
  365.              Structures can contain arrays of other structures which
  366.         in  turn  can  contain  arrays  of  simple  types  or  other
  367.         structures.   It  can go on and on until you lose all reason
  368.         to  continue.   I am only trying to illustrate to  you  that
  369.         structures  are  very valuable and you will find them  great
  370.         aids to programming if you use them wisely.  Be conservative
  371.         at first, and get bolder as you gain experience.
  372.  
  373.              More  complex structures will not be illustrated  here,
  374.         but  you will find examples of additional structures in  the
  375.         example  programs  included  in the  last  chapter  of  this
  376.         tutorial.     For   example,   see   the   "#include"   file
  377.         "STRUCT.DEF".
  378.  
  379.                               WHAT ARE UNIONS?
  380.  
  381.              Load the file named UNION1.C for an example of a union.
  382.         Simply stated,  a union allows you a way to look at the same
  383.         data  with  different types,  or to use the same  data  with
  384.         different names. Examine the program on your monitor.
  385.  
  386.  
  387.  
  388.                                   Page 82
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.                       Chapter 11 - Structures and Unions
  399.  
  400.  
  401.              In this example we have two elements to the union,  the
  402.         first part being the integer named "value",  which is stored
  403.         as  a  two byte variable somewhere in the computers  memory.
  404.         The  second  element is made up of two  character  variables
  405.         named "first" and "second".   These two variables are stored
  406.         in  the  same storage locations that "value" is  stored  in,
  407.         because  that is what a union does.   A union allows you  to
  408.         store  different types of data in the same physical  storage
  409.         locations.  In this case, you could put an integer number in
  410.         "value",  then retrieve it in its two halves by getting each
  411.         half  using  the  two  names  "first"  and  "second".   This
  412.         technique is often used to pack data bytes together when you
  413.         are,  for  example,  combining  bytes  to  be  used  in  the
  414.         registers of the microprocessor.
  415.  
  416.              Accessing  the fields of the union are very similar  to
  417.         accessing the fields of a structure and will be left to  you
  418.         to determine by studying the example.
  419.  
  420.              One  additional  note  must  be given  here  about  the
  421.         program.   When  it is run using the Turbo C  compiler,  the
  422.         data  will  be  displayed with two leading f's  due  to  the
  423.         hexadecimal output promoting the char type variables to  int
  424.         and extending the sign bit to the left.  Converting the char
  425.         type data fields to int type fields prior to display  should
  426.         remove the leading f's from your display.  This will involve
  427.         defining  two new int type variables and assigning the  char
  428.         type  variables to them.  This will be left as  an  exercise
  429.         for  you.  Note that the same problem will come up in a  few
  430.         of the later files also.
  431.  
  432.              Compile and run this program and observe that the  data
  433.         is  read out as an "int" and as two "char"  variables.   The
  434.         "char" variables are reversed in order because of the way an
  435.         "int" variable is stored internally in your computer.  Don't
  436.         worry  about this.  It is not a problem but it can be a very
  437.         interesting area of study if you are so inclined.
  438.  
  439.                            ANOTHER UNION EXAMPLE
  440.  
  441.              Load  and  display the file named UNION2.C for  another
  442.         example of a union,  one which is much more common.  Suppose
  443.         you  wished to build a large database including  information
  444.         on many types of vehicles.  It would be silly to include the
  445.         number of propellers on a car,  or the number of tires on  a
  446.         boat.   In  order to keep all pertinent data,  however,  you
  447.         would  need  those  data points for their  proper  types  of
  448.         vehicles.   In  order to build an efficient data  base,  you
  449.         would need several different types of data for each vehicle,
  450.         some  of which would be common,  and some of which would  be
  451.  
  452.  
  453.  
  454.                                   Page 83
  455.  
  456.  
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463.  
  464.                       Chapter 11 - Structures and Unions
  465.  
  466.  
  467.         different.  That is exactly what we are doing in the example
  468.         program on your monitor.
  469.  
  470.              In this program,  we will define a complete  structure,
  471.         then  decide which of the various types can go into it.   We
  472.         will  start at the top and work our  way  down.   First,  we
  473.         define  a  few constants with the #defines,  and  begin  the
  474.         program  itself.   We define a structure named  "automobile"
  475.         containing  several fields which you should have no  trouble
  476.         recognizing, but we define no variables at this time.
  477.  
  478.                          A NEW CONCEPT, THE TYPEDEF
  479.  
  480.              Next  we  define a new type of data with  a  "typedef".
  481.         This  defines  a complete new type that can be used  in  the
  482.         same way that "int" or "char" can be used.   Notice that the
  483.         structure  has  no name,  but at the end where  there  would
  484.         normally be a variable name there is the name "BOATDEF".  We
  485.         now have a new type, "BOATDEF", that can be used to define a
  486.         structure anyplace we would like to.   Notice that this does
  487.         not  define  any  variables,  only a  new  type  definition.
  488.         Capitalizing  the name is a personal preference only and  is
  489.         not  a  C standard.   It makes the "typedef" look  different
  490.         from a variable name.
  491.  
  492.              We  finally come to the big structure that defines  our
  493.         data using the building blocks already defined  above.   The
  494.         structure is composed of 5 parts, two simple variables named
  495.         "vehicle" and "weight",  followed by the union,  and finally
  496.         the last two simple variables named "value" and "owner".  Of
  497.         course  the union is what we need to look at carefully here,
  498.         so focus on it for the moment.   You will notice that it  is
  499.         composed  of four parts,  the first part being the  variable
  500.         "car" which is a structure that we defined previously.   The
  501.         second  part is a variable named "boat" which is a structure
  502.         of the type "BOATDEF" previously defined.  The third part of
  503.         the  union is the variable "airplane" which is  a  structure
  504.         defined in place in the union.   Finally we come to the last
  505.  
  506.         part  of  the  union,  the variable named  "ship"  which  is
  507.         another structure of the type "BOATDEF".
  508.  
  509.              I  hope  it is obvious to you that all four could  have
  510.         been defined in any of the three ways shown,  but the  three
  511.         different  methods  were used to show you that any could  be
  512.         used.   In practice,  the clearest definition would probably
  513.         have occurred by using the "typedef" for each of the parts.
  514.  
  515.  
  516.  
  517.  
  518.  
  519.  
  520.                                   Page 84
  521.  
  522.  
  523.  
  524.  
  525.  
  526.  
  527.  
  528.  
  529.  
  530.                       Chapter 11 - Structures and Unions
  531.  
  532.  
  533.                             WHAT DO WE HAVE NOW?
  534.  
  535.              We  now have a structure that can be used to store  any
  536.         of  four different kinds of data structures.   The  size  of
  537.         every  record will be the size of that record containing the
  538.         largest  union.   In this case part 1 is the  largest  union
  539.         because  it is composed of three integers,  the others being
  540.         composed  of  an integer and a character  each.   The  first
  541.         member  of this union would therefore determine the size  of
  542.         all structures of this type.  The resulting structure can be
  543.         used to store any of the four types of data, but it is up to
  544.         the  programmer  to  keep track of what is  stored  in  each
  545.         variable of this type.   The variable "vehicle" was designed
  546.         into  this  structure to keep track of the type  of  vehicle
  547.         stored here.   The four defines at the top of the page  were
  548.         designed  to  be  used  as indicators to be  stored  in  the
  549.         variable "vehicle".
  550.  
  551.              A  few  examples of how to use the resulting  structure
  552.         are given in the next few lines of the program.  Some of the
  553.         variables are defined and a few of them are printed out  for
  554.         illustrative purposes.
  555.  
  556.              The union is not used too frequently,  and almost never
  557.         by   beginning   programmers.    You   will   encounter   it
  558.         occasionally  so  it is worth your effort to at  least  know
  559.         what  it is.   You do not need to know the details of it  at
  560.         this time,  so don't spend too much time studying it.   When
  561.         you do have a need for a variant structure, a union, you can
  562.         learn it at that time. For your own benefit, however, do not
  563.         slight the structure. You should use the structure often.
  564.  
  565.                             WHAT IS A BITFIELD?
  566.  
  567.              Load  and display the program named BITFIELD.C  for  an
  568.         example  of how to define and use a bitfield,  a  relatively
  569.         new  addition  to  the  C  programming  language.   In  this
  570.         program,  we  have a union made up of a  single  "int"  type
  571.         variable  in  line 5 and the structure defined  in  lines  6
  572.         through  10.  The structure is composed of  three  bitfields
  573.         named "x", "y", and "z".  The variable named "x" is only one
  574.         bit  wide,the variable "y" is two bits wide and adjacent  to
  575.         the variable "x", and the variable "z" is two bits wide  and
  576.         adjacent  to  "y".  Moreover, because the union  causes  the
  577.         bits  to  be  stored  in the same  memory  location  as  the
  578.         variable "index", the variable "x" is the least  significant
  579.         bit  of the variable "index", "y" is the next two bits,  and
  580.         "z" is the next two.
  581.  
  582.              Compile  and run the program and you will see  that  as
  583.         the  variable "index" is incremented by 1 each time  through
  584.  
  585.  
  586.                                   Page 85
  587.  
  588.  
  589.  
  590.  
  591.  
  592.  
  593.  
  594.  
  595.  
  596.                       Chapter 11 - Structures and Unions
  597.  
  598.  
  599.         the  loop, you will see the bitfields of the union  counting
  600.         due   to  their  respective  locations  within   the   "int"
  601.         definition.
  602.  
  603.              One  thing must be pointed out, the bitfields  must  be
  604.         defined as parts of an "unsigned int" or your compiler  will
  605.         issue an error message.
  606.  
  607.                        WHAT IS THE BITFIELD GOOD FOR?
  608.  
  609.              The  bitfield is very useful if you have a lot of  data
  610.         to  separate  into separate bits or groups  of  bits.   Many
  611.         systems use some sort of a packed format to get lots of data
  612.         stored  in  a  few bytes.  Your  imagination  is  your  only
  613.         limitation to effective use of this feature of C.
  614.  
  615.         PROGRAMMING EXERCISES
  616.  
  617.         1.   Define a named structure  containing a string field for
  618.              a name,  an integer for feet, and another for arms. Use
  619.              the new type to define an array of about 6 items.  Fill
  620.              the fields with data and print them out as follows.
  621.  
  622.              A human being has 2 legs and 2 arms.
  623.              A dog has 4 legs and 0 arms.
  624.              A television set has 4 legs and 0 arms.
  625.              A chair has 4 legs and 2 arms.
  626.              etc.
  627.  
  628.         2.   Rewrite  exercise 1 using  a pointer to print the  data
  629.              out.
  630.  
  631.  
  632.  
  633.  
  634.  
  635.  
  636.  
  637.  
  638.  
  639.  
  640.  
  641.  
  642.  
  643.  
  644.  
  645.  
  646.  
  647.  
  648.  
  649.  
  650.  
  651.  
  652.                                   Page 86
  653.